package rdb

import (
	"fmt"

	"gitee.com/shoyer/logger"
)

var (
	keys = make(map[string]bool)
)

// 用来管理全局的redis key, 防止key重复
func RegisterKeys(keys []string) {
	if len(keys) == 0 {
		return
	}

	for _, k := range keys {
		RegisterKey(k)
	}
}

func RegisterKey(key string) {
	if key == "" {
		logger.Fatalf("key is null")
	}

	if _, ok := keys[key]; ok {
		logger.Fatalf("redis key repeated: %s", key)
	}

	keys[key] = true
}

func GetKey(key string, args ...interface{}) string {
	if _, ok := keys[key]; ok {
		return fmt.Sprintf(key, args...)
	}

	logger.Fatalf("redis key not exist, pls call RegisterKey or RegisterKeys first. : %s", key)
	return ""
}
